<?php
// +----------------------------------------------------------------------
// | 通用PHP框架 [ ThinkPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 郑州智众 [Paulxun@qq.com]
// +----------------------------------------------------------------------
// | 官方网站: https://www.zzmind.com
// +----------------------------------------------------------------------

namespace app\cms\model;

use think\Model as ThinkModel;
use util\Tree;
use think\facade\Db;

/**
 * 导航模型
 * @package app\cms\model
 */
class Nav extends ThinkModel
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'cms_nav';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
	
	/**
	 * 获取导航
	 */
	public static function getNav()
	{
		$return = cache('nav_list');
		if (!$return) {
		    $list_nav = Db::name('cms_nav')->where('status', 1)->column('tag', 'id');
		    $return = [];
		    foreach ($list_nav as $id => $tag) {
		        $data_list = Db::view('cms_menu', true)
		            ->view('cms_column', ['name' => 'column_name'], 'cms_menu.column=cms_column.id', 'left')
		            ->where('cms_menu.nid', $id)
		            ->where('cms_menu.status', 1)
		            ->order('cms_menu.sort,cms_menu.pid,cms_menu.id')
		            ->select()
					->toarray();
		    	
		        foreach ($data_list as &$item) {
		            if ($item['type'] == 0) { // 栏目链接
		                $item['title'] = $item['column_name'];
		                $item['url'] = url('cms/column/list', ['id' => $item['column']]);
		            } else {
		                if ($item['url'] != '#' && substr($item['url'], 0, 4) != 'http') {
		                    $item['url'] = home_url($item['url']);
		                }
		            }
		        }
		    	$return[$tag] = Tree::toLayer($data_list);
		    }
			$return = json_encode($return);
		    // 非开发模式，缓存数据
		    if (config('app.develop_mode') == 0) {
		        cache('nav_list', $return);
		    }
		}
		return json_decode($return, true); 
	}
}
